﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core;
using ZHDJ.Data.BLL;
using ZHDJ.Data.DataCaching;
using ZHDJ.Data.Models.Common;
using ZHDJ.Data.Models.Edu;
using ZHDJ.WebPage;

namespace ZHDJ.Web.Portal.Edu
{
    public partial class PartyEduCoursewareDetail : PortalPage
    {
        #region 页面属性
        public string CourseID
        {
            get
            {
                return this.Request["id"];
            }
        }
        public string CourseTypeName
        {
            get; set;
        }
        public string CourseType
        {
            get; set;
        }

        public List<PartyEduCoursewareImage> Images { get; set; }

        /// <summary>
        /// 视频url
        /// </summary>
        public string FileUrl { get; set; }

        /// <summary>
        /// IsMp4
        /// </summary>
        public string IsMp4 { get; set; }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

            var dt = PartyEduCourseware.GetSingle(CourseID);
            foreach (DataRow dr in dt.Rows)
            {
                this.litCourseType.Text=CourseTypeName = Convert.ToString(dr["CourseTypeName"]);
                CourseType = Convert.ToString(dr["CourseType"]);
                litTitle.Text=this.lblTitle.Text = Convert.ToString(dr["Title"]);
                this.lblTime.Text = Convert.ToDateTime(dr["CreateTime"]).ToString("yyyy-MM-dd HH:mm:ss");
                this.lbBranch.Text = "发布：" + SystemParameterCaching.Get(SystemParameterKey.WebArticleHostName);
                this.ltContent.Text = Convert.ToString(dr["Summary"]);
                this.litCategory.Text= Convert.ToString(dr["CategoryName"]);
                if (!string.IsNullOrEmpty(Convert.ToString(dr["FileID"])))
                {
                    var fileName = UpFileBusiness.GetFileName(Convert.ToString(dr["FileID"]));
                    var url = UpFileBusiness.GetFileUrl(Convert.ToString(dr["FileID"]));
                    this.litFile.Text = string.Format("<a href='{0}' class='alink' title='下载'>{1}</a>", url, fileName);
                    if (fileName.ToLower().EndsWith(".mp4") || fileName.ToLower().EndsWith(".flv"))
                    {
                        this.onlineVideo.Visible = true;
                        FileUrl = url;
                        IsMp4 = FileUrl.ToLower().EndsWith(".mp4") ? "1" : "0";
                    }
                }
                //加载介绍图片
                Images = DB.PartyEduCoursewareImages.Where(a => a.CoursewareID == CourseID).ToList();

                //展示点击数
                this.lblClickCount.Text = Convert.ToString(dr["ClickCount"]);
                var course = DB.PartyEduCoursewares.Find(CourseID);
                course.ClickCount += 1;
                DB.SaveChanges();

                if (!IsPostBack)
                {

                    if (CurrentUser != null)
                    {
                        //增加积分
                        PartyScoreHelper.Add(() => PartyScoreHelper.AddCourseWareClick(CurrentUser.LoginID, Convert.ToString(dr["Title"])));
                    }

                }


                break;
            }

        }
    }
}